package org.dcma.client.widgets;

import java.util.Date;

import org.dcma.client.resources.Resources;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DatePicker;

public class MyDatePicker extends Composite{

	private Label lblFechaInicio;
	private DatePicker datepicker; 
	private PopupPanel calendarpanel;
	private Date selectedDate;
	private Hyperlink btnDate;
	
	@SuppressWarnings("deprecation")
	public MyDatePicker() {
		selectedDate=new Date();
		lblFechaInicio=new Label();
		lblFechaInicio.setStyleName("label_with_border");
		lblFechaInicio.setWidth("74px");
		lblFechaInicio.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		
		datepicker = new DatePicker();
		calendarpanel = new PopupPanel(true);
		calendarpanel.add(datepicker);		
		
		final String dateString = DateTimeFormat.getFormat("dd/MM/yyyy").format(new Date());
        lblFechaInicio.setText(dateString);
		
		datepicker.addValueChangeHandler(new ValueChangeHandler<Date>() {
			
			@Override
			public void onValueChange(ValueChangeEvent<Date> event) {
				selectedDate = event.getValue();
		        String dateString = DateTimeFormat.getFormat("dd/MM/yyyy").format(selectedDate);
		        lblFechaInicio.setText(dateString);		
			}
		});
		btnDate=new Hyperlink(Resources.ICONS.calendar().getHTML(),true,"#");
		btnDate.addStyleName("link-button");
		btnDate.setHeight("22px");
		btnDate.addClickHandler( new ClickHandler() {			
			@Override
			public void onClick(ClickEvent event) {
				Widget source = (Widget) event.getSource();
	            int left = source.getAbsoluteLeft() + 10;
	            int top = source.getAbsoluteTop() + 10;
	            calendarpanel.setPopupPosition(left, top);
				calendarpanel.show();
				
			}
		});
		
		HorizontalPanel fp=new HorizontalPanel();
		fp.setWidth("96px");
		fp.add(lblFechaInicio);
		fp.add(btnDate);		
		initWidget(fp);		
	}
	
	public void setCalendarPanelPosition() {
		calendarpanel.setPopupPosition(btnDate.getAbsoluteLeft(), btnDate.getAbsoluteTop());
	}
	
	public String getSelectedDate() {
		return lblFechaInicio.getText();
	}

	public Date getValue() {		
		return selectedDate;
	}
	
	public void setValue(Date date) {
		this.selectedDate=date;
		lblFechaInicio.setText(DateTimeFormat.getFormat("dd/MM/yyyy").format(selectedDate));
	}
}
